﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Bestfc.Common;

namespace Authority.Bll
{
    public class UsersBll
    {
        private readonly Authority.Dal.UsersDal dal = new Authority.Dal.UsersDal();
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Authority.Model.UsersModel model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Authority.Model.UsersModel model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {

            dal.Delete(Id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Authority.Model.UsersModel GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public Authority.Model.UsersModel GetModelByCache(int Id)
        {

            string CacheKey = "UserModelModel-" + Id;
            object objModel = Bestfc.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = Bestfc.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Bestfc.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Authority.Model.UsersModel)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Authority.Model.UsersModel> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Authority.Model.UsersModel> DataTableToList(DataTable dt)
        {
            List<Authority.Model.UsersModel> modelList = new List<Authority.Model.UsersModel>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Authority.Model.UsersModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Authority.Model.UsersModel();
                    if (dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    model.UserName = dt.Rows[n]["UserName"].ToString();
                    if (dt.Rows[n]["PermissionTypes"].ToString() != "")
                    {
                        model.PermissionTypes = int.Parse(dt.Rows[n]["PermissionTypes"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法
    }
}